package com.anysoft.utils;

import android.text.TextUtils;

import com.anysoft.bean.QueryFundListBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by wwq on 2017/1/16.
 */

public class FundListSortUtils {

    public static ArrayList<QueryFundListBean.DataBean> sortListUp(ArrayList<QueryFundListBean.DataBean> lists, int position, int group) {
        ArrayList<QueryFundListBean.DataBean> list = new ArrayList<QueryFundListBean.DataBean>();
        String[] contents = new String[lists.size()];
        Map<String, String> map = new HashMap<String, String>();
        switch (position) {
            case 0:
                switch (group) {
                    case 0:
                        for (int i = 0; i < lists.size(); i++) {
                            contents[i] = lists.get(i).getPernetvalue() + ":" + lists.get(i).getFundcode();
                            map.put(lists.get(i).getPernetvalue() + ":" + lists.get(i).getFundcode(),
                                    lists.get(i).getFundcode());
                        }
                        break;
                    case 1:
                        for (int i = 0; i < lists.size(); i++) {
                            contents[i] = lists.get(i).getHfincomeratio() + ":" + lists.get(i).getFundcode();
                            map.put(lists.get(i).getHfincomeratio() + ":" + lists.get(i).getFundcode(),
                                    lists.get(i).getFundcode());
                        }
                        break;

                    default:
                        break;
                }
                break;
            case 1:
                switch (group) {
                    case 0:
                        for (int i = 0; i < lists.size(); i++) {
                            contents[i] = lists.get(i).getDayinc() + ":" + lists.get(i).getFundcode();
                            map.put(lists.get(i).getDayinc() + ":" + lists.get(i).getFundcode(), lists.get(i).getFundcode());
                        }
                        break;
                    case 1:
                        for (int i = 0; i < lists.size(); i++) {
                            contents[i] = lists.get(i).getIncomeratio() + ":" + lists.get(i).getFundcode();
                            map.put(lists.get(i).getIncomeratio() + ":" + lists.get(i).getFundcode(),
                                    lists.get(i).getFundcode());
                        }
                        break;

                    default:
                        break;
                }
                break;
            case 2:
                for (int i = 0; i < lists.size(); i++) {
                    contents[i] = lists.get(i).getPeriodWeek() + ":" + lists.get(i).getFundcode();
                    map.put(lists.get(i).getPeriodWeek() + ":" + lists.get(i).getFundcode(), lists.get(i).getFundcode());
                }
                break;
            case 3:
                for (int i = 0; i < lists.size(); i++) {
                    contents[i] = lists.get(i).getPeriodMonthOne() + ":" + lists.get(i).getFundcode();
                    map.put(lists.get(i).getPeriodMonthOne() + ":" + lists.get(i).getFundcode(),
                            lists.get(i).getFundcode());
                }
                break;
            case 4:
                for (int i = 0; i < lists.size(); i++) {
                    contents[i] = lists.get(i).getPeriodMonthThree() + ":" + lists.get(i).getFundcode();
                    map.put(lists.get(i).getPeriodMonthThree() + ":" + lists.get(i).getFundcode(),
                            lists.get(i).getFundcode());
                }
                break;
            case 5:
                for (int i = 0; i < lists.size(); i++) {
                    contents[i] = lists.get(i).getPeriodMonthSix() + ":" + lists.get(i).getFundcode();
                    map.put(lists.get(i).getPeriodMonthSix() + ":" + lists.get(i).getFundcode(),
                            lists.get(i).getFundcode());
                }
                break;
            case 6:
                for (int i = 0; i < lists.size(); i++) {
                    contents[i] = lists.get(i).getPeriodSinceYear() + ":" + lists.get(i).getFundcode();
                    map.put(lists.get(i).getPeriodSinceYear() + ":" + lists.get(i).getFundcode(),
                            lists.get(i).getFundcode());
                }
                break;
            case 7:
                for (int i = 0; i < lists.size(); i++) {
                    contents[i] = lists.get(i).getPeriodSinceEstablish() + ":" + lists.get(i).getFundcode();
                    map.put(lists.get(i).getPeriodSinceEstablish() + ":" + lists.get(i).getFundcode(),
                            lists.get(i).getFundcode());
                }
                break;

            default:
                break;
        }

        for (int i = 0; i < contents.length; i++) {
        }
        String[] sortStrings = new String[lists.size()];
        for (int i = 0; i < contents.length; i++) {
            sortStrings[i] = contents[i];
        }
        for (int i = 0; i < contents.length; i++) {
            for (int j = 0; j < contents.length - i - 1; j++) {
                String strJ = sortStrings[j].split(":")[0];
                double numJ = (TextUtils.isEmpty(strJ) || "null".equals(strJ)|| "--".equals(strJ)) ? 0.0
                        : Double.parseDouble(strJ.replace("%", ""));
                String strJ1 = sortStrings[j + 1].split(":")[0];
                double numJ1 = (TextUtils.isEmpty(strJ1) || "null".equals(strJ1)|| "--".equals(strJ1)) ? 0.0
                        : Double.parseDouble(strJ1.replace("%", ""));
                if (numJ > numJ1) {
                    String str = sortStrings[j];
                    sortStrings[j] = sortStrings[j + 1];
                    sortStrings[j + 1] = str;
                }
            }
        }

        for (int i = 0; i < sortStrings.length; i++) {
        }

        ArrayList<String> newList = new ArrayList<String>();
        for (int i = 0; i < sortStrings.length; i++) {
            newList.add(map.get(sortStrings[i]));
        }

        for (int i = 0; i < newList.size(); i++) {
        }

        for (int i = 0; i < newList.size(); i++) {
            for (int j = 0; j < lists.size(); j++) {
                if (newList.get(i).equals(lists.get(j).getFundcode())) {
                    list.add(lists.get(j));
                    break;
                }
            }
        }

        for (int i = 0; i < list.size(); i++) {
        }

        return list;
    }

    public static ArrayList<QueryFundListBean.DataBean> sortListDown(ArrayList<QueryFundListBean.DataBean> lists, int position, int group) {
        ArrayList<QueryFundListBean.DataBean> list = new ArrayList<QueryFundListBean.DataBean>();
        String[] contents = new String[lists.size()];
        Map<String, String> map = new HashMap<String, String>();
        switch (position) {
            case 0:
                switch (group) {
                    case 0:
                        for (int i = 0; i < lists.size(); i++) {
                            contents[i] = lists.get(i).getPernetvalue() + ":" + lists.get(i).getFundcode();
                            map.put(lists.get(i).getPernetvalue() + ":" + lists.get(i).getFundcode(),
                                    lists.get(i).getFundcode());
                        }
                        break;
                    case 1:
                        for (int i = 0; i < lists.size(); i++) {
                            contents[i] = lists.get(i).getHfincomeratio() + ":" + lists.get(i).getFundcode();
                            map.put(lists.get(i).getHfincomeratio() + ":" + lists.get(i).getFundcode(),
                                    lists.get(i).getFundcode());
                        }
                        break;

                    default:
                        break;
                }
                break;
            case 1:
                switch (group) {
                    case 0:
                        for (int i = 0; i < lists.size(); i++) {
                            contents[i] = lists.get(i).getDayinc() + ":" + lists.get(i).getFundcode();
                            map.put(lists.get(i).getDayinc() + ":" + lists.get(i).getFundcode(), lists.get(i).getFundcode());
                        }
                        break;
                    case 1:
                        for (int i = 0; i < lists.size(); i++) {
                            contents[i] = lists.get(i).getIncomeratio() + ":" + lists.get(i).getFundcode();
                            map.put(lists.get(i).getIncomeratio() + ":" + lists.get(i).getFundcode(),
                                    lists.get(i).getFundcode());
                        }
                        break;

                    default:
                        break;
                }
                break;
            case 2:
                for (int i = 0; i < lists.size(); i++) {
                    contents[i] = lists.get(i).getPeriodWeek() + ":" + lists.get(i).getFundcode();
                    map.put(lists.get(i).getPeriodWeek() + ":" + lists.get(i).getFundcode(), lists.get(i).getFundcode());
                }
                break;
            case 3:
                for (int i = 0; i < lists.size(); i++) {
                    contents[i] = lists.get(i).getPeriodMonthOne() + ":" + lists.get(i).getFundcode();
                    map.put(lists.get(i).getPeriodMonthOne() + ":" + lists.get(i).getFundcode(),
                            lists.get(i).getFundcode());
                }
                break;
            case 4:
                for (int i = 0; i < lists.size(); i++) {
                    contents[i] = lists.get(i).getPeriodMonthThree() + ":" + lists.get(i).getFundcode();
                    map.put(lists.get(i).getPeriodMonthThree() + ":" + lists.get(i).getFundcode(),
                            lists.get(i).getFundcode());
                }
                break;
            case 5:
                for (int i = 0; i < lists.size(); i++) {
                    contents[i] = lists.get(i).getPeriodMonthSix() + ":" + lists.get(i).getFundcode();
                    map.put(lists.get(i).getPeriodMonthSix() + ":" + lists.get(i).getFundcode(),
                            lists.get(i).getFundcode());
                }
                break;
            case 6:
                for (int i = 0; i < lists.size(); i++) {
                    contents[i] = lists.get(i).getPeriodSinceYear() + ":" + lists.get(i).getFundcode();
                    map.put(lists.get(i).getPeriodSinceYear() + ":" + lists.get(i).getFundcode(),
                            lists.get(i).getFundcode());
                }
                break;
            case 7:
                for (int i = 0; i < lists.size(); i++) {
                    contents[i] = lists.get(i).getPeriodSinceEstablish() + ":" + lists.get(i).getFundcode();
                    map.put(lists.get(i).getPeriodSinceEstablish() + ":" + lists.get(i).getFundcode(),
                            lists.get(i).getFundcode());
                }
                break;

            default:
                break;
        }
        for (int i = 0; i < contents.length; i++) {
        }
        String[] sortStrings = new String[lists.size()];
        for (int i = 0; i < contents.length; i++) {
            sortStrings[i] = contents[i];
        }
        for (int i = 0; i < contents.length; i++) {
            for (int j = 0; j < contents.length - i - 1; j++) {
                String strJ = sortStrings[j].split(":")[0];
                double numJ = (TextUtils.isEmpty(strJ) || "null".equals(strJ)|| "--".equals(strJ)) ? 0.0
                        : Double.parseDouble(strJ.replace("%", ""));
                String strJ1 = sortStrings[j + 1].split(":")[0];
                double numJ1 = (TextUtils.isEmpty(strJ1) || "null".equals(strJ1) || "--".equals(strJ1)) ? 0.0
                        : Double.parseDouble(strJ1.replace("%", ""));
                if (numJ < numJ1) {
                    String str = sortStrings[j];
                    sortStrings[j] = sortStrings[j + 1];
                    sortStrings[j + 1] = str;
                }
            }
        }

        for (int i = 0; i < sortStrings.length; i++) {
        }

        ArrayList<String> newList = new ArrayList<String>();
        for (int i = 0; i < sortStrings.length; i++) {
            newList.add(map.get(sortStrings[i]));
        }

        for (int i = 0; i < newList.size(); i++) {
        }

        for (int i = 0; i < newList.size(); i++) {
            for (int j = 0; j < lists.size(); j++) {
                if (newList.get(i).equals(lists.get(j).getFundcode())) {
                    list.add(lists.get(j));
                    break;
                }
            }
        }

        for (int i = 0; i < list.size(); i++) {
        }

        return list;
    }
}
